package io.jpress.front.controller;

import com.jfinal.plugin.activerecord.Record;
import io.jpress.core.BaseFrontController;
import io.jpress.model.core.Jdb;
import io.jpress.router.RouterMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/9.
 */

/**
 * 评论聊天
 */

@RouterMapping(url = "/messages")
public class MessagesController extends BaseFrontController {


    public void index(){
        String key = getPara("key");
        if (key == null || key.length()==0){
            List<Record> datas = new ArrayList<>();
            ResponseBean response = new ResponseBean();
            response.setData(datas);
            response.setCode(0);
            renderJson(response);
            return;
        }
        String sql = "select m.fid fid, m.fkey fkey, \n" +
                "m.fmessageinfo fmessageinfo, m.ftype ftype, \n" +
                "m.ftitle ftitle, m.fcreatetime fcreatetime \n" +
                "from `jpress_messages` m where m.fkey = '"+ key +"'";
        ResponseBean responseBean = new ResponseBean();

        List<Record> list = Jdb.find(sql);
        responseBean.setData(list);
        renderJson(responseBean);
    }

    public void submit() {
        String fkey = getPara("key");
        String fmessageinfo = getPara("fmessageinfo");
        String ftitle = getPara("ftitle");
        String ftype = getPara("ftype");


        String sql = "/*dialect*/insert into `jpress`.`jpress_messages` (fid, \n" +
                "fkey, fmessageinfo, ftitle, fcreatetime, ftype) values \n" +
                "(unix_timestamp(now()) , '"+fkey+"', '"+fmessageinfo+"', '"+ftitle+"', \n" +
                "date_format(now(), '%Y-%m-%d %H:%i:%s') , '"+ftype+"')";
        Jdb.update(sql);
        List<Record> datas = new ArrayList<>();
        ResponseBean response = new ResponseBean();
        response.setData(datas);
        response.setCode(0);
        renderJson(response);
    }

}
